﻿import QtQuick 2.11
import QtGraphicalEffects 1.0

import "."
import "_Global"
import "_Global/Popup"
import "_Global/Button"

import "_Global/Global.js" as GlobalJS
import "GameJS.js" as GameJS

/*
  选择Level的界面
*/

//1个Class
Item {
    signal s_StartGame(int cID, int lID, int restart)
    signal s_close()
    signal showConfig()

    property int nColCount: 5   //列
    property int nRowCount: 4   //行

    property alias levelInfo: levelInfo


    id: root
    anchors.fill: parent

    Image { //背景
        id: imageBack
        source: Global._FixResourcePath_R("Media/Images/Class/Back.png")
        anchors.fill: parent
    }

    ImageButton2 {  //关闭
        id: imageReturn
        x: Global.gamePos.mapClass.rectReturn.x
        y: Global.gamePos.mapClass.rectReturn.y
        width: Global.gamePos.mapClass.rectReturn.width
        height: Global.gamePos.mapClass.rectReturn.height

        source: Global._FixResourcePath_R("Media/Images/Return.png")
        soundEffectClick: Global.resource.effects.effectClose
        bEffectOn: Global.config.bEffectOn
        onButtonClicked: {
            s_close();
        }
    }

    ImageButton2 {  //设置
        x: Global.dpW(1820)
        y: Global.dpH(0)
        width: Global.dpW(100)
        height: Global.dpH(100)
        source: Global._FixResourcePath_R("Media/Images/Exit/Config.png")
        soundEffectClick: Global.resource.effects.effectButton
        bEffectOn: Global.config.bEffectOn
        onButtonClicked: {
            showConfig();
        }
    }


    Image { //标题
        id: imageTitle
        source: Global._FixResourcePath_R("Media/Images/Class/Title.png")
        x: Global.gamePos.mapClass.rectTitle.x
        y: Global.gamePos.mapClass.rectTitle.y
        width: Global.gamePos.mapClass.rectTitle.width
        height: Global.gamePos.mapClass.rectTitle.height
    }

    Text {  //提示文字
        id: totalScoreText
        font.pixelSize: Global.dpH(24);
        style: Text.Outline;
        styleColor: "yellow"
        horizontalAlignment: Text.AlignHCenter
        anchors.horizontalCenter: parent.horizontalCenter
        text: qsTr("您目前总积分：%1，等级：%2").arg(UserInfo.scoreCount).arg(Global.userInfo.userLevel);
        //verticalAlignment: Text.AlignVCenter
        visible: false  //!!!
    }


    ImageButton2 { //上一关
        id: buttonPrior
        x: Global.gamePos.mapClass.rectLeft.x
        y: Global.gamePos.mapClass.rectLeft.y
        width: Global.gamePos.mapClass.rectLeft.width
        height: Global.gamePos.mapClass.rectLeft.height
        source: Global._FixResourcePath_R("Media/Images/Class/Prior.png")
        soundEffectClick: Global.resource.effects.effectPage
        bEffectOn: Global.config.bEffectOn

        onButtonClicked: {
            if(_private.nPage > 0) {
                _private.nPage--;
                refreshAllLevels();
            }
        }
    }
    ImageButton2 { //下一关
        id: buttonNext
        x: Global.gamePos.mapClass.rectRight.x
        y: Global.gamePos.mapClass.rectRight.y
        width: Global.gamePos.mapClass.rectRight.width
        height: Global.gamePos.mapClass.rectRight.height
        source: Global._FixResourcePath_R("Media/Images/Class/Next.png")
        soundEffectClick: Global.resource.effects.effectPage
        bEffectOn: Global.config.bEffectOn

        onButtonClicked: {
            if(_private.nPage < _private.nPageCount - 1) {
                _private.nPage++;
                refreshAllLevels();
            }
        }
    }


    //弹出详细窗口
    GameLevelInfo {
        id: levelInfo
        //x: 200; y: 180
        z: 30
        innerWidth: Global.gamePos.dialogMap.rectBack.width
        innerHeight: Global.gamePos.dialogMap.rectBack.height
        visible: false
        anchors.verticalCenter: parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
        //anchors.horizontalCenterOffset: -Global.gamePos.sizeMsgBox.width/2;

        maskParent: root

        onS_StartGame: root.s_StartGame( cID, lID, restart );
    }


    //每个Level
    Component {
        id: levelRectComp
        Rectangle {
            //property var levelInfoParam: new Object
            property alias bGray: colorOverlay.visible  //灰色不可点
            property var levelInfoParam: {   //level的信息数据
                "levelID": 0,
                "hasStudy": 0,   //0,1,2 没学,学习（有u）,复习（有n）
                "wordCount": 0,
                "unStudiedCount": 0,
                "mistakeCount": 0,
                "correctCount": 0,
                "wordRecords": null
            }

            id: levelRect
            radius: 20
            color: '#00000000'
            width: Global.gamePos.mapClass.sizeLevelRect.width
            height: Global.gamePos.mapClass.sizeLevelRect.height

            Image {
                id: backImage
                source: Global._FixResourcePath_R("Media/Images/Class/ClassBack.png")
                anchors.fill: parent
            }

            //颜色遮罩(不可用时黑色,source必须和本对象同一层次!!!)
            ColorOverlay {
                id: colorOverlay
                visible: true
                anchors.fill: backImage;
                source: backImage;
                color: "#80000000"
            }

            Text {  //关卡数
                id: levelIDText
                text: (parent.levelInfoParam.levelID + 1)
                //anchors.fill: parent
                font {
                    //family: Global.resource.fonts.font1.name
                    pixelSize: Global.gamePos.mapClass.nLevelFontSize
                }

                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter

                //horizontalAlignment: Text.AlignHCenter
                //verticalAlignment: Text.AlignVCenter
            }
            /*
            Text {
                id: total
                //text: parent.wordCount
                anchors.right: parent.right
                anchors.bottom: parent.bottom
            }
            Text {
                id: correct
                //text: parent.correctCount
                anchors.bottom: parent.bottom
                anchors.left: parent.left
            }*/

            Image { //小红点
                id: tixing
                width: Global.gamePos.mapClass.sizeRemind.width
                height: Global.gamePos.mapClass.sizeRemind.height
                //source: Global._FixResourcePath_R("Media/Images/Class/Red.png")
                //border.color: "black"
                //border.width: 5
                anchors.right: parent.right
                anchors.top: parent.top
                anchors.rightMargin: Global.dpW(-14)
                anchors.topMargin: Global.dpH(-12)
                visible: false
            }

            MouseArea {
                anchors.fill: parent
                //onClicked: s_StartGame()
                onClicked: {

                    _private.showLevelInfo(parent.levelInfoParam)
                    //console.debug(parent.levelInfoParam.levelID)
                }
            }

            //Level 方块样式 小红点和背景
            function refreshLevelRect() {

                levelIDText.text = levelInfoParam.levelID + 1

                if(levelInfoParam.hasStudy == 2) {
                    //levelRect.color = 'green'
                    levelRect.bGray = false;
                    if(levelInfoParam.wordCount == levelInfoParam.correctCount) {
                        tixing.visible = false;
                    }
                    else {
                        tixing.visible = true
                        tixing.source = Global.resource.images.gameClass.red
                    }
                }
                else if(levelInfoParam.hasStudy == 1){
                    tixing.visible = true
                    tixing.source = Global.resource.images.gameClass.yellow
                    //levelRect.color = 'green'
                    levelRect.bGray = false;
                }
                else {
                    tixing.visible = false
                    //levelRect.color = 'gray'
                    levelRect.bGray = true;
                }
            }
        }
    }



    //创建 一页 关卡的Level数量(nRowCount和nColCount改变时要调用)
    function createPageLevelCount() {

        var i, obj;
        //创建所有关卡图块
        for(i = _private.rectArray.length; i < nColCount * nRowCount; i++) {
            obj = levelRectComp.createObject(root);
            obj.visible = false;
            _private.bindingLevelRectPos(obj,i);
            _private.rectArray.push(obj);
        }

        //删除多余的
        i = nColCount * nRowCount;
        while(i < _private.rectArray.length) {
            _private.rectArray.pop().destroy();
        }

    }

    //刷新 classID下 的level个数并刷新,外部可以调用多次
    function refreshLevelsFromClassID(classID) {

        _private.nClassID = classID
        _private.levelCount = GameManager.userInfo.studyInfo.classInfoMap[classID].classLevelCount

        _private.nPageCount = Math.ceil(_private.levelCount / nColCount / nRowCount)
        _private.nPage = 0

        //console.debug("_private.nPageCount:", _private.nPageCount)
        //console.debug("_private.levelCount:", _private.levelCount)

        refreshAllLevels();

        return _private.levelCount;
    }

    //从GameCore中刷新当前页的所有Level块和信息,外部可以调用多次
    function refreshAllLevels() {

        //console.debug("levelCount",_private.levelCount)
        var i, obj;
        var showedLevelCount;   //当前页显示多少个Level
        if(_private.nPage + 1 == _private.nPageCount) {
            showedLevelCount = _private.levelCount % (nColCount * nRowCount)
            if(showedLevelCount == 0)
                showedLevelCount = nColCount * nRowCount
        }
        else
            showedLevelCount = nColCount * nRowCount

        //console.debug("showedLevelCount:",showedLevelCount)
        //刷新所有关卡图块
        for(i = 0; i < showedLevelCount; i++) {

            obj = _private.rectArray[i];

            obj.visible = true;
            obj.levelInfoParam.levelID = i + nColCount * nRowCount * _private.nPage;

            _private.refreshALevelInfo(_private.nClassID, obj.levelInfoParam.levelID, i);
            obj.refreshLevelRect();

        }

        //多余的隐藏
        for(; i < nColCount * nRowCount; i++) {
            _private.rectArray[i].visible = false;
        }


        //第一关没学,则设置为可学习
        if(_private.rectArray[0].levelInfoParam.hasStudy == 0 && _private.nPage == 0) {
            _private.rectArray[0].levelInfoParam.hasStudy = 1;
            _private.rectArray[0].refreshLevelRect();
        }

        //读取 GameManager.userInfo 并 设置每个level信息
        //for(var v in GameManager.userInfo.studyInfo.classInfoMap[_private.nClassID].levelList) {
            //console.debug("levelID2:",v)
        //    _private.refreshALevelInfo(_private.nClassID,v);
        //    _private.rectArray[v].refreshLevelRect();

            //console.debug('第' + v +'关:', _private.rectArray[v].levelInfoParam.hasStudy,_private.rectArray[v].levelInfoParam.correctCount,_private.rectArray[v].levelInfoParam.mistakeCount,_private.rectArray[v].levelInfoParam.wordCount,_private.rectArray[v].levelInfoParam.wordRecords)

            //console.debug(v + "1:::::::" + GameManager.userInfo.studyInfo.classInfoMap[1].levelList)
            //console.debug(_private.rectArray[v].levelInfoParam.hasStudy)
            //console.debug(_private.rectArray[v].levelInfoParam.wordCount)
        //}

        /*
        console.debug("-------------aaa----------------")
        console.debug(GameManager.userInfo.studyInfo.classInfoMap)
        */

        //刷新关卡模块
        //for(i = 0; i < _private.levelCount; i++) {
        //    _private.refreshALevelInfo(_private.nClassID,i);
        //    _private.rectArray[i].refreshLevelRect();
            //console.debug("level",_private.rectArray[i].levelInfoParam.levelID,":",_private.rectArray[i].levelInfoParam.hasStudy)

        //}


    }

    function refreshClass() {   //刷新文字
        //totalScoreText.text =
    }

    QtObject {
        id: _private
        property int nClassID         //本课程ID
        property int nCurrentLevelID: -1    //目前玩的关卡ID
        property int levelCount      //level数目
        property var rectArray: []      //level对象列表

        property int nPage: 0   //页
        property int nPageCount: 0   //共n页


        //从GameCore读取信息 刷新 一个Level块
        function refreshALevelInfo(classID,levelID,levelRectID) {
            //console.debug("refreshALevelInfo:",classID,levelID)
            if(GameManager.userInfo.studyInfo.classInfoMap[classID] == undefined)
                return;

            //if(levelID >= GameManager.userInfo.studyInfo.classInfoMap[classID].levelList.length) {
            if(GameManager.userInfo.studyInfo.classInfoMap[classID].levelMap[levelID] == undefined) {
                _private.rectArray[levelRectID].levelInfoParam.hasStudy = 0;
                _private.rectArray[levelRectID].levelInfoParam.wordCount = 0;
                _private.rectArray[levelRectID].levelInfoParam.correctCount = 0;
                _private.rectArray[levelRectID].levelInfoParam.mistakeCount = 0;
                _private.rectArray[levelRectID].levelInfoParam.unStudiedCount = 0;
                _private.rectArray[levelRectID].levelInfoParam.wordRecords = "";

                return;
            }

            var tLevelInfo = GameManager.userInfo.studyInfo.classInfoMap[classID].levelMap[levelID];
            //console.debug("levelID:",levelID)
            _private.rectArray[levelRectID].levelInfoParam.wordCount = tLevelInfo.wordCount;
            _private.rectArray[levelRectID].levelInfoParam.correctCount = tLevelInfo.correctCount;
            _private.rectArray[levelRectID].levelInfoParam.mistakeCount = tLevelInfo.mistakeCount;
            _private.rectArray[levelRectID].levelInfoParam.unStudiedCount = tLevelInfo.unStudiedCount;

            _private.rectArray[levelRectID].levelInfoParam.wordRecords = tLevelInfo.wordRecords;
            //if(_private.rectArray[v].levelInfoParam.wordCount == _private.rectArray[levelRectID].levelInfoParam.correctCount + _private.rectArray[levelRectID].levelInfoParam.mistakeCount) {

            //显示为复习或学习
            if(_private.rectArray[levelRectID].levelInfoParam.unStudiedCount == 0
                    && _private.rectArray[levelRectID].levelInfoParam.wordRecords != '')
                _private.rectArray[levelRectID].levelInfoParam.hasStudy = 2;
            else
                _private.rectArray[levelRectID].levelInfoParam.hasStudy = 1;


            _private.rectArray[levelRectID].refreshLevelRect();
            //console.debug(_private.rectArray[levelRectID].levelInfoParam.wordCount,_private.rectArray[levelRectID].levelInfoParam.correctCount,_private.rectArray[levelRectID].levelInfoParam.mistakeCount,_private.rectArray[levelRectID].levelInfoParam.wordRecords)
        }

        //显示一个levelID信息
        function showLevelInfo(levelInfoParam) {
            //var param = new Object;
            //param.level = levelID
            //if(levelID < GameManager.userInfo.studyInfo.classInfoMap[classID].levelList.length) {
            //    param.hasStudy = true
            //    param.totalCount = GameManager.userInfo.studyInfo.classInfoMap[classID].levelList[levelID].wordCount
            //    param.correntCount = GameManager.userInfo.studyInfo.classInfoMap[classID].levelList[levelID].correctCount
           //}
           // else {
           //     param.hasStudy = false
            //}

            //console.debug(levelID,"2:::::",_private.rectArray[levelID].levelInfoParam,_private.rectArray[levelID].levelInfoParam.levelID)
            //console.debug(_private.rectArray[levelID].levelInfoParam.hasStudy)
            //console.debug(_private.rectArray[levelID].levelInfoParam.wordCount)
            //console.debug(_private.rectArray[levelID].levelInfoParam.correctCount)


            levelInfo.refreshLevelInfo(levelInfoParam)
            levelInfo.show()
        }

        function bindingLevelRectPos(obj,i) {
            obj.x = Qt.binding(function(){return (i % nColCount) * Global.gamePos.mapClass.rectExcursion.width + Global.gamePos.mapClass.rectExcursion.x});
            obj.y = Qt.binding(function(){return parseInt(i / nColCount) * Global.gamePos.mapClass.rectExcursion.height + Global.gamePos.mapClass.rectExcursion.y});
        }
    }


    Component.onCompleted: {
        createPageLevelCount();
    }

}
